 <template>
  <div class="current-page">
    <div>
      <el-button style="margin-bottom: 10px;float:left;padding-right: 20px" type="primary">
        高级搜索
      </el-button>
      <div class="search-btn">
        <el-button style="margin-bottom: 10px;float:right;padding-right: 20px" type="text" @click="exprotData"
          icon="el-icon-download">导出
        </el-button>
      </div>
    </div>
    <el-table :data="tableData" border style="width: 100%" :height="tableHeight" :cell-style="{ textAlign: 'center' }"
      :header-cell-style="{textAlign: 'center'}">
      <el-table-column type="index" label="序号" width="50">
      </el-table-column>
      <el-table-column label="用户账号">
        <template slot-scope="scope">
          <span>{{ scope.row.Subject.UserAccount }}</span>
        </template>
      </el-table-column>
      <el-table-column label="计算机名称" width="180">
        <template slot-scope="scope">
          <span>{{ scope.row.Subject.ComputerName }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="Action" label="动作类型"> </el-table-column>
      <el-table-column label="服务名称">
        <template slot-scope="scope">
          <span>{{ scope.row.Object.name }}</span>
        </template>
      </el-table-column>
      <!-- 
      <el-table-column label="服务类型">
        <template slot-scope="scope">
          <span>{{ scope.row.Object.Service.ServiceType }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="ProgramPath" label="程序路径">
        <template slot-scope="scope">
          <span>{{ scope.row.Object.Service.ProgramPath }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="ServiceName" label="公司名称">
        <template slot-scope="scope">
          <span>{{ scope.row.Object.Service.ServiceName }}</span>
        </template>
      </el-table-column> -->
      <el-table-column prop="Reason" label="原因"> </el-table-column>
      <el-table-column prop="Result" label="登录结果"> </el-table-column>
      <el-table-column prop="Level" label="事件级别">
        <template slot-scope="scope">
          <span>{{ scope.row.Level|objectText}}</span>
        </template>

      </el-table-column>
      <el-table-column prop="Handle" label="处理方式"> </el-table-column>
    </el-table>
    <el-pagination style="padding-top: 10px;text-align: center;" @size-change="handleSizeChange"
      @current-change="handleCurrentChange" :current-page="pageNum" :page-sizes="[10, 20, 30, 40]"
      :page-size=" pageSize" layout="total, sizes, prev, pager, next, jumper" :total="totalCount">
    </el-pagination>

  </div>
</template>
<script>
import ComTable from "../../components/table/comTable";
export default {
  name: "cmf_tplate",
  data() {
    return {
      pageSizes: [5, 10, 20, 50, 100],
      hasData: true,
      dialogControl: false,
      dialogTitle: "",
      tableHeadList: [
        { columnComment: "主机唯一标识", columnName: "HostCode", colHidden: true, },
        { columnComment: "用户账号", columnName: "UserAccount", },
        { columnComment: "计算机名称", columnName: "ComputerName", },
        { columnComment: "动作类型", columnName: "Action", },
        // { columnComment: "服务类型", columnName: "ServiceType", },
        // { columnComment: "程序路径", columnName: "ProgramPath", },
        // { columnComment: "服务名称", columnName: "ServiceName", },
        // { columnComment: "公司名称", columnName: "ServiceName", },
        { columnComment: "原因", columnName: "Reason", },
        { columnComment: "登录结果", columnName: "Result", },
        { columnComment: "事件级别", columnName: "Level", typeCollection: "LevelEnum" },
        { columnComment: "处理方式", columnName: "Handle", },


      ],
      pageNum: 1,
      pageSize: 50,
      totalCount: 0,
      boolean: [],
      tableData: [], // 表格数据
      tableHeight: "", // 表格高度
      loading: false, // 数据加载状态遮罩
      tableDataSelected: [],
      operateTime: [],
      xproPath: '',
      searchForm: {
        warnState: "",
        warnRank: "",
        warnContent: "",
        objType: "",
      },
      infoForm: {
        from: '本地'
      },
      attrData: [],
      rules: {
        // 表单验证规则
        name: [{ required: true, trigger: "blur", message: "请输入名称" }],
      },
      emwCheckList: [],
    };
  },
  components: {
    ComTable,
  },
  created() {
    this.setTableHeight();
    this.getTableData()
  },
  filters: {
    //枚举--等级告警
    objectText(item) {

      console.log(item, "item");
      switch (item.toString()) {
        case "0":
          return "紧急";
        case "1":
          return "报警";
        case "2":
          return "关键";
        case "3":
          return "错误";
        case "4":
          return "警告";
        case '5':
          return "通知";
        case "6":
          return "信息";
        case "7":
          return "调试";

        default:
          return '---';
      }
    },
  },
  methods: {
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageSize = val
      this.getTableData()

    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.pageNum = val
      this.getTableData()
    },
    getTableData() {
      this.$get(`sjrz/log/service?page=${this.pageNum}&count=${this.pageSize}`).then(jsonData => {
        console.log(jsonData.data.data, "sss");

        this.totalCount = jsonData.data.page.total

        // let tableData = [
        //   {
        //     "Root": {
        //       "LogType": "17",
        //       "HostCode": "aec538e45d2f24de90b8e898f5096b16",
        //       "TimeStamp": "1664346610",
        //       "Trigger": {
        //         "type": "策略",
        //         "name": "服务监控策略",
        //         "Trigger": ""
        //       },
        //       "Subject": {
        //         "class": "计算机",
        //         "name": "localhost.localdomain",
        //         "UserAccount": "root",
        //         "ComputerName": "localhost.localdomain"
        //       },
        //       "Action": "开启",
        //       "Object": {
        //         "class": "服务",
        //         "name": "cups",
        //         "Service": {
        //           "UserAccount": "12",
        //           "ServiceType": "123",
        //           "ProgramPath": "1234",
        //           "ServiceName": "125",
        //           "ComputerName": "555",
        //           "ServiceDescrilbe": "666"
        //         }
        //       },
        //       "Reason": "",
        //       "Level": 2,
        //       "Handle": "审计",
        //       "Result": "成功"
        //     }
        //   },
        //   {
        //     "Root": {
        //       "LogType": "17",
        //       "HostCode": "aec538e45d2f24de90b8e898f5096b16",
        //       "TimeStamp": "1664346610",
        //       "Trigger": {
        //         "type": "策略",
        //         "name": "服务监控策略",
        //         "Trigger": ""
        //       },
        //       "Subject": {
        //         "class": "计算机",
        //         "name": "localhost.localdomain",
        //         "UserAccount": "root",
        //         "ComputerName": "localhost.localdomain"
        //       },
        //       "Action": "关闭",
        //       "Object": {
        //         "class": "服务",
        //         "name": "cups",
        //         "Service": {
        //           "UserAccount": "",
        //           "ServiceType": "",
        //           "ProgramPath": "",
        //           "ServiceName": "",
        //           "ComputerName": "",
        //           "ServiceDescrilbe": ""
        //         }
        //       },
        //       "Reason": "",
        //       "Level": 2,
        //       "Handle": "审计",
        //       "Result": "成功"
        //     }
        //   },
        //   {
        //     "Root": {
        //       "LogType": "17",
        //       "HostCode": "aec538e45d2f24de90b8e898f5096b16",
        //       "TimeStamp": "1664346610",
        //       "Trigger": {
        //         "type": "策略",
        //         "name": "zj123",
        //         "Trigger": ""
        //       },
        //       "Subject": {
        //         "class": "计算机",
        //         "name": "localhost.localdomain",
        //         "UserAccount": "root",
        //         "ComputerName": "localhost.localdomain"
        //       },
        //       "Action": "开启",
        //       "Object": {
        //         "class": "服务",
        //         "name": "cups",
        //         "Service": {
        //           "UserAccount": "",
        //           "ServiceType": "",
        //           "ProgramPath": "",
        //           "ServiceName": "",
        //           "ComputerName": "",
        //           "ServiceDescrilbe": ""
        //         }
        //       },
        //       "Reason": "",
        //       "Level": 2,
        //       "Handle": "审计",
        //       "Result": "成功"
        //     }
        //   },
        //   {
        //     "Root": {
        //       "LogType": "17",
        //       "HostCode": "aec538e45d2f24de90b8e898f5096b16",
        //       "TimeStamp": "1664346610",
        //       "Trigger": {
        //         "type": "策略",
        //         "name": "zj123",
        //         "Trigger": ""
        //       },
        //       "Subject": {
        //         "class": "计算机",
        //         "name": "localhost.localdomain",
        //         "UserAccount": "root",
        //         "ComputerName": "localhost.localdomain"
        //       },
        //       "Action": "关闭",
        //       "Object": {
        //         "class": "服务",
        //         "name": "cups",
        //         "Service": {
        //           "UserAccount": "",
        //           "ServiceType": "",
        //           "ProgramPath": "",
        //           "ServiceName": "",
        //           "ComputerName": "",
        //           "ServiceDescrilbe": ""
        //         }
        //       },
        //       "Reason": "",
        //       "Level": 2,
        //       "Handle": "审计",
        //       "Result": "成功"
        //     }
        //   },
        //   {
        //     "Root": {
        //       "LogType": "17",
        //       "HostCode": "aec538e45d2f24de90b8e898f5096b16",
        //       "TimeStamp": "1664346610",
        //       "Trigger": {
        //         "type": "策略",
        //         "name": "zj123",
        //         "Trigger": ""
        //       },
        //       "Subject": {
        //         "class": "计算机",
        //         "name": "localhost.localdomain",
        //         "UserAccount": "root",
        //         "ComputerName": "localhost.localdomain"
        //       },
        //       "Action": "开启",
        //       "Object": {
        //         "class": "服务",
        //         "name": "cups",
        //         "Service": {
        //           "UserAccount": "",
        //           "ServiceType": "",
        //           "ProgramPath": "",
        //           "ServiceName": "",
        //           "ComputerName": "",
        //           "ServiceDescrilbe": ""
        //         }
        //       },
        //       "Reason": "",
        //       "Level": 2,
        //       "Handle": "审计",
        //       "Result": "成功"
        //     }
        //   },
        //   {
        //     "Root": {
        //       "LogType": "17",
        //       "HostCode": "aec538e45d2f24de90b8e898f5096b16",
        //       "TimeStamp": "1664346610",
        //       "Trigger": {
        //         "type": "策略",
        //         "name": "zj123",
        //         "Trigger": ""
        //       },
        //       "Subject": {
        //         "class": "计算机",
        //         "name": "localhost.localdomain",
        //         "UserAccount": "root",
        //         "ComputerName": "localhost.localdomain"
        //       },
        //       "Action": "关闭",
        //       "Object": {
        //         "class": "服务",
        //         "name": "cups",
        //         "Service": {
        //           "UserAccount": "",
        //           "ServiceType": "",
        //           "ProgramPath": "",
        //           "ServiceName": "",
        //           "ComputerName": "",
        //           "ServiceDescrilbe": ""
        //         }
        //       },
        //       "Reason": "",
        //       "Level": 2,
        //       "Handle": "审计",
        //       "Result": "成功"
        //     }
        //   }
        // ]
        jsonData.data.data.forEach(item => {
          this.tableData.push(item.Root)
        })

      })
    },

    refreshTable() {
      if (this.hasData) {
        this.getTableData(this.pageNum, this.pageSize);
      } else {
        // 调用子组件中的getTableData
        this.$refs.comTable.getTableData();
      }
    },
    //重新加载表格
    setSearchHeight() {
      //搜索区域高度
      if (this.$refs.search !== undefined) {
        this.$nextTick(() => {
          let searchHeight = this.$refs.search.offsetHeight;
          this.setTableHeight(searchHeight);
        });
      }
    },


    /**
     * 设置表格的高度
     */
    setTableHeight() {
      let clientHeiht =
        window.innerHeight || document.documentElement.clientHeight;
      this.tableHeight = clientHeiht - 160;
    },
    headerCellStyle({ row, column, rowIndex, columnIndex }) {
      return {
        "font-size": "14px",
        "font-weight": 600,
        color: "#606266",
      };
    },
    //查询
    //todo 待后台接口
    search() { },

    //新建与修改
    openDialog(type, row) {
      this.dialogControl = true;
      console.log(type);
      if (type === "add") {
        this.dialogTitle = "新增应用资源";
      } else if (type === "update") {
        this.dialogTitle = "修改应用资源";
        console.log(row, "row");
        let form = JSON.parse(JSON.stringify(row))
        console.log(form, 'qqqq');
        this.xproPath = form.XPRO_PATH,
          this.infoForm = {
            name: form.name,
            uuid: form.uuid,
            ip: form.ip,
            port: form.port,
            type: [],
            from: '本地'
          };
        if (form.type.length > 0) {
          form.type.forEach(item => {
            this.infoForm.type = item.XPRO_PATH
          })
        }
      }
    },

    /**
     * 新增
     */
    addTerminal() {
      this.$refs.infoForm.validate(flag => {
        if (flag) {
          let params = {};
          for (let item in this.infoForm) {
            if (this.infoForm[item]) {
              if (item == 'type') {
                if (this.infoForm[item].length !== 0) {
                  console.log(item, this.infoForm[item], 'item789')
                  params[item] = [this.infoForm[item]];
                }
              } else {
                params[item] = this.infoForm[item];
              }
            }
          }
          console.log(params);
          this.loading = true;
          this.$postForJson('app', params).then(jsonData => {
            this.dialogControl = false;
            this.loading = false;
            let datas = jsonData.data;
            if (!datas.success) {
              this.publicModal.$emit('tips', {
                show: true,
                publicModalMessage: datas.msg,
                fontIcon: 'errorWarning'
              })
              return false
            }
            this.$message({
              message: '新增文本文件信息成功！',
              type: 'success'
            });
            this.refreshTable();
          })
        }
      })
    },

    /**
     * 修改
     */
    modifyTerminal() {
      this.$refs.infoForm.validate(flag => {
        if (flag) {
          let params = {};
          for (let item in this.infoForm) {
            if (this.infoForm[item]) {
              if (item == 'type') {
                if (this.infoForm[item].length !== 0) {
                  console.log(item, this.infoForm[item], 'item789')
                  params[item] = [this.infoForm[item]];
                }
              } else {
                params[item] = this.infoForm[item];
              }
            }
          }
          let url = this.xproPath;
          this.loading = true;
          console.log(params, 'params')
          this.$postForJson(url, params).then((jsonData) => {
            this.dialogControl = false;
            this.loading = false;
            let datas = jsonData.data;
            if (!datas.success) {
              this.publicModal.$emit('tips', {
                show: true,
                publicModalMessage: datas.msg,
                fontIcon: 'errorWarning'
              })
              return false
            }
            this.$message({
              message: '修改文本文件信息成功',
              type: 'success'
            });
            this.refreshTable();
          })

        }
      })
    },
    //重置
    clearData() {
      this.searchForm = {};
    },
  },
};
</script>
<style scoped>
</style>
